package singlelist;

import listinterface.IList;
import myexception.ListIndexOutOfBoundsException;

public class MySingList implements IList {

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

        public int getVal() {
            return val;
        }

        public ListNode getNext() {
            return next;
        }
    }

    public ListNode head;//表示当前链表的头节点

    public int listSize;

    //手动创建一个链表
    public void creatList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }


    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
        listSize++;
//        if (head == null){
//            head = node;
//        }else {
//            //对于插入来说，我们一般建议 先绑定后边
//            node.next = head;
//            head = node;
//        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            return;
        }

        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
        listSize++;
    }

    @Override
    public void addIndex(int index, int data) throws ListIndexOutOfBoundsException {
        ListNode node = new ListNode(data);

        checkIndexOfAdd(index);

        if (index == 0) {
            addFirst(data);
            return;
        }

        if (index == size()) {
            addLast(data);
            return;
        }

        ListNode cur = findIndex(index);

        node.next = cur.next;
        cur.next = node;
        listSize++;
    }

    private void checkIndexOfAdd(int index) throws ListIndexOutOfBoundsException {
        if (index < 0 || index > size()) {
            throw new ListIndexOutOfBoundsException("输入的Index位置不合法");
        }
    }

    //找到index-1位置节点的地址
    private ListNode findIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (head == null) {
            return;
        }

        if (head.val == key) {
            head = head.next;
            listSize--;
            return;
        }

        ListNode cur = findNode(key);
        if (cur == null) {
            System.out.println("没有你要找的数据");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;

        listSize--;
    }

    //找到第一个遇到的key的前一个节点
    //没有找到返回null
    private ListNode findNode(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;

    }

    @Override
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }

//        while (head.val == key){
//            head = head.next;
//        }

        ListNode cur = head.next;
        ListNode prev = head;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
            } else {
                prev = cur;
            }
            cur = cur.next;
        }
        if (head.val == key) {
            head = head.next;
        }
    }

    @Override
    public int size() {
//        int count = 0;
//        ListNode cur = head;
//
//        while (cur != null) {
//            count++;
//            cur = cur.next;
//        }
//        return count;
        return listSize;
    }

    @Override
    public void clear() {
        head = null;
        listSize = 0;

    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void display(MySingList.ListNode newHead) {
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public ListNode reverseList() {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }

        ListNode cur = head.next;
        head.next = null;

        while (cur != null) {
            ListNode cueNext = cur.next;
            cur.next = head;
            head = cur;
            cur = cueNext;
        }
        return head;
    }

    public ListNode middleNode() {
        //常规版
        if (head == null) {
            return head;
        }
        int len = size();
        int index = len / 2;
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    public ListNode middleNode1() {
//        if (head == null){
//            return null;
//        }
//        ListNode fast = head;
//        ListNode slow = head;
//        while (fast != null){
//            fast = fast.next;
//            if (fast != null){
//                slow = slow.next;
//                fast = fast.next;
//            }else {
//                break;
//            }
//        }
//        return slow;
        if (head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public int kthToLast(int k) {
        if (k <= 0) {
            return -1;
        }

        ListNode fast = head;
        ListNode slow = head;

        int count = 0;
        while (count != k - 1) {
            fast = fast.next;
            if (fast == null) {
                return -1;
            }
            count++;
        }
        //同时走
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;

//        while (fast.next != null) {
//            fast = fast.next;
//            if (fast == null) {
//                return -1;
//            }
//            k--;
//            if (k <= 0) {
//                slow = slow.next;
//            }
//        }
//        return slow.val;

    }

    public ListNode partition(int x) {
        ListNode bs = null;
        ListNode be = null;

        ListNode as = null;
        ListNode ae = null;

        ListNode cur = head;

        while (cur != null) {
            if (cur.val < x) {
                if (bs == null) {
                    bs = be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if (bs == null) {
            return as;
        }
        be.next = as;
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }

    public boolean chkPalindrome() {
        if (head == null) {
            return false;
        }
        if (head.next == null) {
            return true;
        }
        //找到链表的一半部分
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }

        //翻转
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }

        //head从头往后，slow从后往前
        while (head != slow) {
            if (head.val != slow.val) {
                return false;
            }
            //判断偶数情况
            if (head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
    public boolean hasCycle(){
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast= fast.next.next;
            slow = slow.next;
            if (fast == slow){
                return true;
            }
        }
        return false;
    }
    public void createLoop(){
        ListNode cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = head.next.next.next;
    }

    public ListNode detectCycle(ListNode head){
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast= fast.next.next;
            slow = slow.next;
            if (fast == slow){
                break;
            }
        }

        if (fast ==null||fast.next==null){
            return null;
        }

        fast = head;
        while (fast != slow){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}
